#!/bin/bash
#########################################################
#                                                       #
#           D-Star Repeater Service Handler             #
#                                                       #
# Written for Pi-Star (http://www.mw0mwz.co.uk/pi-star) #
#               By Andy Taylor (MW0MWZ)                 #
#                                                       #
#                     Version 1.3                       #
#                                                       #
#########################################################

# Service Config
DAEMON=dstarrepeaterd
DAEMON_PATH=/usr/local/bin/
DAEMON_OPTS="-daemon"
CONFIG=/etc/dstarrepeater
PGREP=/usr/bin/pgrep
KILL=/bin/kill
SLEEP=/bin/sleep
USER=root
GROUP=mmdvm
LOGDIR=/var/log/pi-star
ipVar=`hostname -I | cut -d' ' -f1`

# Pre-flight checks...
test -x ${DAEMON_PATH}${DAEMON} || exit 0
test -r $CONFIG || exit 0

# if MMDVMHost is configured or running, dont start this daemon!
! test -r /etc/dstar-radio.mmdvmhost || exit 0
test -r /etc/dstar-radio.dstarrepeater || exit 0

if [[ $(egrep -h -i "1234567|M1ABC" ${CONFIG} | wc -l) -gt 0 ]]; then
  exit 0;
fi

# if ircDDBGAteway is configured for Icom Repeater, dont run DStarRepeater
if [[ $(grep "repeaterType1" /etc/ircddbgateway | cut -c 15) -ge 1 ]]; then
  exit 0;
fi

if [ `$PGREP "MMDVMHost"` ]; then
  echo "Service 'MMDVMHost' is already running, cannot start $DAEMON"
  exit 0;
fi

# Verify the logging directory exists, if not create it and setup the ownership / permissions
if [ ! -d $LOGDIR ]; then
  mkdir -p $LOGDIR
  chown ${USER}:${GROUP} $LOGDIR
  chmod 775 $LOGDIR
fi

case "$1" in
	start)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
			exit 0;
		else
			# Wait for an IP Address
			until [ "$ipVar" != " " ]; do
				sleep 10
				ipVar=`hostname -I`
			done
			if [[ $(grep "Version" /etc/pistar-release | cut -c 11) -ge 4 ]]; then
				nice -n -10 ${DAEMON_PATH}${DAEMON} & disown
				$PGREP $DAEMON > /run/dstarrepeater.pid
			else
				nice -n -10 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS}
			fi
			echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
			exit 0;
		fi
		;;

	stop)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			$KILL `${PGREP} ${DAEMON}`
			exit 0;
		else
			echo -e "$DAEMON is not running"
			exit 0;
		fi
		;;

	restart)
		if [ `$PGREP $DAEMON` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			$KILL `${PGREP} ${DAEMON}`
			$SLEEP 3
			if [[ $(grep "Version" /etc/pistar-release | cut -c 11) -ge 4 ]]; then
				nice -n -10 ${DAEMON_PATH}${DAEMON} & disown
				$PGREP $DAEMON > /run/dstarrepeater.pid
			else
				nice -n -10 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS}
			fi
			echo -e "$DAEMON re-started as PID "`${PGREP} ${DAEMON}`
			exit 0;
		else
			echo -e "$DAEMON is not running"
			if [[ $(grep "Version" /etc/pistar-release | cut -c 11) -ge 4 ]]; then
				nice -n -10 ${DAEMON_PATH}${DAEMON} & disown
				$PGREP $DAEMON > /run/dstarrepeater.pid
			else
				nice -n -10 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS}
			fi
			echo -e "$DAEMON started as PID "`${PGREP} ${DAEMON}`
			exit 0;
		fi
		;;

	status)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is running as PID "`${PGREP} ${DAEMON}`
		else
			echo -e "$DAEMON is not running"
		fi
		;;

	*)
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 0
esac
